package com.example.phoenixshop.service.impl;

import com.example.phoenixshop.bean.User;
import com.example.phoenixshop.dao.UserMapper;
import com.example.phoenixshop.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;


    @Override
    public User findUserByAccount(String account) {
        try {
            return userMapper.selectUserByAccount(account);
        }catch (Exception e) {
            return null;
        }
    }

    @Override
    public User getUserInfoByAccountWithoutPwd(String account) {
        try {
            User user = userMapper.selectUserByAccount(account);
            user.setPassword(null);
            return user;
        }catch (Exception e) {
            return null;
        }
    }

    /**
     * 用户登录
     * @param account
     * @param password
     * @return
     */
    @Override
    public User login(String account, String password) {
        try {
            return userMapper.selectUserByAccountAndPwd(account,password);
        }catch (Exception e) {
            return null;
        }
    }

    @Override
    public int register(User user) {
        try {
            if(null==userMapper.selectUserByAccount(user.getAccount())){
                return userMapper.insertSelective(user);
            }else{
                return 0;
            }
        }catch (Exception e) {
            return -1;
        }
    }
}
